#include "InitSockets.h"

bool InitSockets()
{
	if(!CStarted)
	{
		bool res = (WSAStartup(MAKEWORD( 2, 0 ), &W) == 0);
		if(res)
			CStarted = 1;
		return res;
	}
	CStarted++;
	return true;
}

sockaddr_in socketAddr(const char* addr)
{
	unsigned int  A,B,C,D,Port, IP;
	if((sscanf(addr, "%d.%d.%d.%d:%d", &A, &B, &C, &D, &Port) < 5) || (A > 255) ||
		(B > 255) || (C > 255) || (D > 255) || (Port>65535) || !Port)
	{
		IP = 0;
		Port = 0;
	}
	IP = D + C * 0x100 + B * 0x10000 + A * 0x1000000;

	sockaddr_in sockAddr;
	sockAddr.sin_family = AF_INET;
	sockAddr.sin_port = htons(Port);
	if (Port == 0)
		sockAddr.sin_addr.s_addr = INADDR_NONE;
	else
	{
		if (IP == 0) sockAddr.sin_addr.s_addr = INADDR_ANY;
		else sockAddr.sin_addr.s_addr = htonl(IP);
	}
	return sockAddr;
}
